<template>
  <div class="mod-sys__user">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <ren-dept-tree v-model="state.dataForm.deptId" :placeholder="$t('dept.title')" :query="true"></ren-dept-tree>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.username" :placeholder="$t('user.username')" clearable
          style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.fullName" placeholder="姓名" clearable style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.remark" placeholder="备注" clearable style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
      <!--
        <el-button v-if="state.hasPermission('sys:user:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add")
          }}</el-button>
          -->
          <el-button type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
      <!--
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:user:setExh')" type="primary" @click="exhibitionlogo()"
          :disabled="state.single">展会授权</el-button>
      </el-form-item>
      -->
      <el-form-item>
      <!--
        <el-button v-if="state.hasPermission('sys:user:forbidden')" type="danger" @click="forbiddenHandle()">{{
          $t("forbidden") }}</el-button>
          -->
          <el-button type="danger" @click="forbiddenHandle()">{{
                    $t("forbidden") }}</el-button>
      </el-form-item>
      <!-- <el-form-item>
        <el-button v-if="state.hasPermission('sys:user:delete')" type="danger" @click="state.deleteHandle()">{{
          $t("deleteBatch") }}</el-button>
      </el-form-item> -->
      <!--
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:user:export')" type="info" @click="state.exportHandle()">{{
          $t("export") }}</el-button>
      </el-form-item>
      -->
    </el-form>
    <el-table :header-cell-style="{ 'whiteSpace': 'nowrap' }" v-loading="state.dataListLoading" :data="state.dataList"
      border @selection-change="state.dataListSelectionChangeHandle" @sort-change="state.dataListSortChangeHandle"
      style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="40"
        show-overflow-tooltip></el-table-column>
      <el-table-column prop="userId" label="用户id" sortable="custom" header-align="center" align="center"
        show-overflow-tooltip></el-table-column>
      <el-table-column prop="username" :label="$t('user.username')" sortable="custom" header-align="center"
        align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="deptName" :label="$t('user.deptName')" header-align="center" align="center"
        show-overflow-tooltip></el-table-column>
      <el-table-column prop="fullName" label="姓名" header-align="center" align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="remark" label="备注" header-align="center" align="center"
        show-overflow-tooltip></el-table-column>
      <el-table-column prop="mobile" :label="$t('user.mobile')" sortable="custom" header-align="center" align="center"
        show-overflow-tooltip></el-table-column>
      <el-table-column prop="seatUser" label="坐席账号" sortable="custom" header-align="center" align="center"
        show-overflow-tooltip></el-table-column>
      <el-table-column prop="status" :label="$t('user.status')" sortable="custom" header-align="center" align="center"
        width="120" show-overflow-tooltip>
        <template v-slot="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">{{ $t("user.status0") }}</el-tag>
          <el-tag v-else size="small" type="success">{{ $t("user.status1") }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" sortable="custom" header-align="center" align="center" width="180"
        show-overflow-tooltip></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="120"
        show-overflow-tooltip>
        <template v-slot="scope">
        <!--
          <el-button v-if="state.hasPermission('sys:user:update')" type="primary" link
            @click="addOrUpdateHandle(scope.row.userId)">{{ $t("update") }}</el-button>
          <el-button v-if="state.hasPermission('sys:user:delete')" type="primary" link
            @click="state.deleteHandle(scope.row.userId)">{{ $t("delete") }}</el-button>
            -->
            <el-button type="primary" link
                        @click="addOrUpdateHandle(scope.row.userId)">{{ $t("update") }}</el-button>
                      <el-button type="primary" link
                        @click="state.deleteHandle(scope.row.userId)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
      :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle"
      @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="refreshDataList"></add-or-update>
    <!-- 展会授权 -->
    <Exhibition ref="ExhibitionlogoRef" @refreshDataList="refreshDataList"></Exhibition>

  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { reactive, ref, toRefs } from "vue";
import AddOrUpdate from "./user-add-or-update.vue";
import Exhibition from "./exhibition-logo.vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";

const view = reactive({
  getDataListURL: "/sys/user/page",
  getDataListIsPage: true,
  deleteURL: "/sys/user",
  deleteIsBatch: true,
  exportURL: "/sys/user/export",
  dataForm: {
    username: "",
    deptId: "",
    postId: "",
    gender: "",
    remark: "",
    fullName:""

  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateRef = ref();
const addOrUpdateHandle = (userId?: number) => {
  addOrUpdateRef.value.init(userId);
};

//展会授权
const ExhibitionlogoRef = ref();
const exhibitionlogo = (userId?: number) => {
  // 获取选中数据
  let ids: string[] = [];
  for (let i = 0; i < state.dataListSelections!.length; i++) {
    ids.push(state.dataListSelections![i].userId);
  }
  if (ids == null || ids.length <= 0) {
    return ElMessage.warning("请选择操作项");
  }
  ExhibitionlogoRef.value.init(ids);
};

const forbiddenHandle = () => {
  let ids: string[] = [];
  for (let i = 0; i < state.dataListSelections!.length; i++) {
    ids.push(state.dataListSelections![i].userId);
  }
  if (ids == null || ids.length <= 0) {
    return ElMessage.warning("请选择操作项");
  }
  return baseService.put("/sys/user/updateStatus", ids).then((res) => {
    if (res.code == 0) {
      // 重新获取数据列表
      state.getDataList();
      return ElMessage.success("禁用账号成功");
    }
  });
}
//刷新数据
const refreshDataList = () => {
  state.refreshDataList();
};

</script>
